﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using GameSchoolWeb.Models;
using System.Web.Security;

namespace GameSchoolWeb.Controllers
{
	[Authorize]
	public class AssignmentController : Controller
	{
		ICourseRepository m_repository;


		public AssignmentController()
		{
			m_repository = new CourseRepository();
		}

		public AssignmentController(ICourseRepository rep)
		{
			m_repository = rep;
		}

		public ActionResult Index()
		{
			return View();
		}

		[HttpGet]
		public ActionResult CreateAssignment()
		{
			return View(new Assignment());
		}


		[HttpPost]
		public ActionResult CreateAssignment(FormCollection formdata, int? id)
		{
			if (id == null) return View("NotFound");
			Assignment assignmentItem = new Assignment();
			UpdateModel(assignmentItem);
			assignmentItem.levelId = id.Value;
			assignmentItem.dDateAdded = DateTime.Now;
			m_repository.AddAssignment(assignmentItem);
			m_repository.Save();

			return RedirectToAction("GetAssignmentsByLevelId", new { id = id.Value });
		}

		[HttpGet]
		public ActionResult GetAssignmentsByLevelId(int? id)
		{

			if (id == null) return View("NotFound");

			// assignments er listi af verkefnum í núverandi borði.
			var assignments = from a in m_repository.Assignments()
							  where a.levelId == id.Value
							  orderby a.dDateAdded ascending
							  select a;
			// lvlId er núverandi borð
			int lvlId = id.Value;

			AssignmentListModelView vm = new AssignmentListModelView(assignments, lvlId);

			return View(vm);

		}

		[Authorize]
		[HttpGet]
		public ActionResult TurnInAssignment(int? id)
		{
			if (id == null) return View("NotFound");
			var model = (from c in m_repository.Assignments()
						 where c.id == id
						 select c).SingleOrDefault();

			// userGuid er auðkenni núverandi notanda            
			Guid userGuid = (Guid)Membership.GetUser().ProviderUserKey;

			// assignmentCheck nær í upplýsingar hvort nemandi hafið lokið verkefninu.
			TurninInAssignment assignmentCheck = (from t in m_repository.TurnInAssignment()
												  where t.UserId == userGuid && t.assignmentId == id.Value
												  select t).SingleOrDefault();

			GetAssignmentModelView vm = new GetAssignmentModelView(model, new TurninInAssignment(), assignmentCheck);
			return View(vm);
		}
		[Authorize]
		[HttpPost]
		public ActionResult TurnInAssignment(FormCollection formdata, int? id)
		{
			if (id == null) return View("NotFound");
			TurninInAssignment TurnInAssignment = new TurninInAssignment();
			GetAssignmentModelView vm = new GetAssignmentModelView(new Assignment(), new TurninInAssignment(), new TurninInAssignment());
			UpdateModel(vm);

			var assignmentTitle = (from c in m_repository.Assignments()
								   where c.id == id.Value
								   select c.strTitle).SingleOrDefault();

			// sett er inn rétt gögn þegar nemandi skilar inn.
			Guid userGuid = (Guid)Membership.GetUser().ProviderUserKey;
			TurnInAssignment.UserId = userGuid;
			TurnInAssignment.assignmentId = id.Value;
			TurnInAssignment.strContent = vm.TurnInAssignment.strContent;
			TurnInAssignment.dReturnDate = DateTime.Now;

			TurnInAssignment.strTitle = assignmentTitle;
			m_repository.TurnInAssignment(TurnInAssignment);
			m_repository.Save();
			return RedirectToAction("GetAssignmentsByLevelId", new { id = TurnInAssignment.Assignment.levelId });
		}
		[Authorize]
		public ActionResult GetAllTurnedInAssignment(int? id)
		{
			if (id == null) return View("NotFound");
			var model = from c in m_repository.TurnInAssignment()
						where c.assignmentId == id.Value
						select c;
			return View(model);
		}

		[Authorize]
		[HttpGet]
		public ActionResult PostStudentScore(TurninInAssignment turnInAssignmentItem)
		{
			// tek inn objectið turninAssignmentItem 
			// sem inniheldur rétt assignmentId og rétt Userid
			var assignment = turnInAssignmentItem;

			// model er valda verkefnið sem valinn nemandi skilaði inn.
			var model = (from c in m_repository.TurnInAssignment()
						 where c.assignmentId == assignment.assignmentId && c.UserId == assignment.UserId
						 select c).SingleOrDefault();

			return View(model);
		}

		[Authorize]
		[HttpPost]
		public ActionResult PostStudentScore(FormCollection formdata)
		{
			TurninInAssignment turnInAssignment = new TurninInAssignment();
			UserRegistration userReg = new UserRegistration();
			UpdateModel(turnInAssignment);

			// turnIn er yfirfarið verkefni nemanda.
			var turnIn = (from t in m_repository.TurnInAssignment()
						  where t.assignmentId == turnInAssignment.assignmentId && t.UserId == turnInAssignment.UserId
						  select t).SingleOrDefault();

			if (turnIn != null)
			{
				turnIn.nScore = turnInAssignment.nScore;
				m_repository.Save();
				return RedirectToAction("GetAllTurnedInAssignment", new { id = turnInAssignment.assignmentId });
			}
			return View("NotFound");
		}
	}
}
